// ==UserScript==
// @name           InsertXMPP
// @namespace      xmpp4gg
// @description    Inserts XMPP code into iframe gadgets on iGoogle page
// @include        http://www.google.com/ig
// @include        *.gmodules.com/*
// ==/UserScript==

var userName;

function insertXMPP ()
{
	
	if (window.location.hostname == "www.google.com") 
	{
		var userElement = document.getElementById("guser");

		if (userElement == null) {
			logMessage("Unable to get username");
			//ERROR
		} else {
			var elements = userElement.textContent.split("|");
		
			for (var i = 0; i < elements.length; i++) {
				if (elements[i].match(/.+@gmail\.com/)) {
					userName = elements[i].split("@")[0];
					//GM_setValue("XMPP4GGUser", userName);
					break;
				}
			}
		}

		
		//RADI:
		if(GM_getValue("iGoogleWindowCounter") != null)
		{
			var numWindows;
			numWindows = GM_getValue("iGoogleWindowCounter");
			if(numWindows<=0)
			{
				numWindows = 1;
			}
			else
			{
				numWindows++;
			}
			GM_setValue("iGoogleWindowCounter",numWindows);
		}
		
		if(GM_getValue("XMPP4GGUser")==null || GM_getValue("XMPP4GGUser")=='0')
		{
			userName = prompt("Upisite zeljeno korisnicko ime za gadgete", userName);
			GM_setValue("XMPP4GGUser", userName);
		}
		
		/*
		if(GM_getValue("iGoogleWindowCounter") == null || GM_getValue("iGoogleWindowCounter")<=0)
		{
			GM_setValue("iGoogleWindowCounter", 1);
		}
		if(GM_getValue("iGoogleWindowCounter") == 1 && (GM_getValue("XMPP4GGUser")==null || GM_getValue("XMPP4GGUser")=='0'))
		{
			userName = prompt("Upisite zeljeni prefiks resursima na stranici", userName);
			GM_setValue("XMPP4GGUser", userName);
		}*/
		/*
		if(GM_getValue("XMPPUserName")==null || GM_getValue("XMPPUserName")=='0')
		{
			var xmppUserName = prompt("Upisite XMPP korisnicko ime", userName);
			GM_setValue("XMPPUserName",xmppUserName);
		}
		if(GM_getValue("XMPPPassword")==null || GM_getValue("XMPPPassword")=='0')
		{
			var xmppPassword = prompt("Upisite XMPP lozinku", ' ');
			GM_setValue("XMPPPassword", xmppPassword);
		}*/
		
	} else {
		userName = GM_getValue("XMPP4GGUser"); // + GM_getValue("iGoogleWindowCounter");
		var xmppUserName = GM_getValue("XMPPUserName");
		var xmppPassword = GM_getValue("XMPPPassword");
		var xmppRegister = GM_getValue("XMPPRegister");
		//alert(xmppRegister);

		var headElement = document.getElementsByTagName("head").item(0);

//		var importLog4js = document.createElement("script");
//		importLog4js.type = "text/javascript";
//		importLog4js.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/log4js.js";
//		headElement.appendChild(importLog4js);

		var importPrototype = document.createElement("script");
		importPrototype.type = "text/javascript";
		importPrototype.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/prototype.js";
		headElement.appendChild(importPrototype);

		var importDom = document.createElement("script");
		importDom.type = "text/javascript";
		importDom.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/dom-all.js";
		headElement.appendChild(importDom);

		var importCrypto = document.createElement("script");
		importCrypto.type = "text/javascript";
		importCrypto.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/crypto.js";
		headElement.appendChild(importCrypto);

		var importXMPP4JS = document.createElement("script");
		importXMPP4JS.type = "text/javascript";
		importXMPP4JS.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/xmpp4js.js";
		headElement.appendChild(importXMPP4JS);
		
		var importRpc4XMPP = document.createElement("script");
		importRpc4XMPP.type = "text/javascript";
		importRpc4XMPP.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/rpc4xmpp.js";
		headElement.appendChild(importRpc4XMPP);

		var importXMPP4GG = document.createElement("script");
		importXMPP4GG.type = "text/javascript";
		importXMPP4GG.src = "http://xmpp4gg.googlecode.com/svn/trunk/lib/xmpp4gg.js";
		headElement.appendChild(importXMPP4GG);
		
		var xmppCode = document.createElement("script");
		xmppCode.innerHTML += "client.login('" + xmppUserName +"', '" + xmppPassword +"', '161.53.65.135', '" + window.name + "', 'http://161.53.65.135:7070/http-bind/'," + xmppRegister + "); client.logMessage('" + window.name + " connected.');";
		//xmppCode.innerHTML += 'rpcxmpp.register("Add", Add);';
		headElement.appendChild(xmppCode);
				
		document.getElementsByTagName("body")[0].innerHTML += "Pristupno sucelje:<br/>" + xmppUserName + "@161.53.65.135/" + window.name;
		//var rpcxmpp = new rpcxmpp();
		//rpcxmpp.register("Add", Add);
		
		
		
	}
}

function insertHtml()
{
	var guser, xmppuser;
	
	guser = document.getElementById('guser');
	if(guser)
	{
		xmppuser = document.createElement('div');
		xmppuser.id = 'xmppuser';
		xmppuser.align='right';
		xmppuser.width='100%';
		
		if(GM_getValue('XMPPUserName')!=null && GM_getValue('XMPPUserName')!='0')
		{
			xmppuserNobrUsername = document.createElement('nobr');
			xmppuserNobrUsername.innerHTML = 'XMPP korisnik : ';
			
			//Put bold tag
			xmppuserBUsername = document.createElement('b');
			xmppuserBUsername.innerHTML = GM_getValue('XMPPUserName') + ' | ';
			
			xmppuserLogoutUsername = document.createElement('a');
			xmppuserLogoutUsername.href='#';
			xmppuserLogoutUsername.addEventListener('click', function(){GM_setValue("XMPPUserName", '0'); window.location.href=unescape(window.location.pathname);}, false);
			xmppuserLogoutUsername.innerHTML = 'Logout';
			
			xmppuserNobrUsername.appendChild(xmppuserBUsername);
			xmppuserNobrUsername.appendChild(xmppuserLogoutUsername);
			xmppuser.appendChild(xmppuserNobrUsername);
			
		}
		else if (GM_getValue('XMPPUserName')==null || GM_getValue('XMPPUserName')=='0')
		{
			xmppuserNobrLogin = document.createElement('nobr');
			
			xmppuserNobrLogin.innerHTML = 'XMPP korisnicko ime : ';
			xmppuserTbUsername = document.createElement('input');
			xmppuserTbUsername.type = 'text';
			xmppuserTbUsername.id = 'xmppuserTbUsername';
			xmppuserNobrLogin.appendChild(xmppuserTbUsername);
			
			xmppuserNobrLogin.innerHTML += ' Lozinka : ';
			xmppuserTbPassword = document.createElement('input');
			xmppuserTbPassword.type='password';
			xmppuserTbPassword.id = 'xmppuserTbPassword';
			xmppuserNobrLogin.appendChild(xmppuserTbPassword);
			
			xmppuserNobrLogin.innerHTML += ' Registriraj me : ';
			xmppuserCbRegister = document.createElement('input');
			xmppuserCbRegister.type = 'checkbox';
			xmppuserCbRegister.id = 'xmppuserCbRegister';
			xmppuserNobrLogin.appendChild(xmppuserCbRegister);
			
			xmppuserLoginUsername = document.createElement('a');
			xmppuserLoginUsername.href='#';
			xmppuserLoginUsername.addEventListener('click', function() {GM_setValue("XMPPUserName", document.getElementById("xmppuserTbUsername").value); GM_setValue("XMPPPassword", document.getElementById("xmppuserTbPassword").value); GM_setValue("XMPPRegister", document.getElementById("xmppuserCbRegister").checked); window.location.href=unescape(window.location.pathname);},false);
			xmppuserLoginUsername.innerHTML = 'Login';
			xmppuserNobrLogin.appendChild(xmppuserLoginUsername);
			
			xmppuser.appendChild(xmppuserNobrLogin);
			
		}
		guser.parentNode.appendChild(xmppuser);
		
		//alert("jijnk");
	}
	
}

insertHtml();
insertXMPP();
